﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOFinish : DevExpress.XtraEditors.XtraForm
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        DataTable dt;
        public event EventHandler Reload;
        private LogUtils log;

        public frm_MOFinish()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void ctrlMOFinish_Load(object sender, EventArgs e)
        {
            dt = clsMODAO.GetMOFinishMONo();
            List<ClsMOTable> objs = new List<ClsMOTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsMOTable obj = new ClsMOTable();
                obj.MONo = r["MONo"].ToString();  //
                obj.PartID = r["PartID"].ToString();   ////
                obj.MOQty = int.Parse(r["MOQty"].ToString());////
                obj.DeadLine = DateTime.Parse(r["DeadLine"].ToString());     ////
                obj.Finished = (bool)r["Finished"];  ////
                obj.OKQty = (int)r["OKQty"];////
                obj.NGQty = (int)r["NGQty"];////
                obj.Finishdate = DateTime.Parse(r["Finishdate"].ToString());
                objs.Add(obj);
            }
            
            cbMONo.DataSource = objs;
            cbMONo.DisplayMember = "MONo";

            //
            //ClsPartDataDAO clsPart = new ClsPartDataDAO();
            //dt = clsPart.GetAllPartID();
            //cbPartID.DataSource = dt;
            //cbPartID.DisplayMember = "PartNo";
            //cbPartID.ValueMember = "PartNo";

        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            ClsMOTable obj = ((ClsMOTable)cbMONo.SelectedItem);
            cbMONo.Text = obj.MONo.ToString();
            
            cbPartID.Text = obj.PartID.ToString();
            cbPartID.Visible = true;
            txtOKQty.Text = obj.OKQty.ToString();
            txtOKQty.Visible = true;
            txtNGQty.Text = obj.NGQty.ToString();
            txtNGQty.Visible = true;
            txtMOQty.Text = obj.MOQty.ToString();
            txtMOQty.Visible = true;
            checkFinish.Checked = obj.Finished;
            checkFinish.Visible = true;
            dateFinish.Value = obj.Finishdate;
            dateFinish.Visible = true;
            label6.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label10.Visible = true;
            label3.Visible = true;
            label8.Visible = true;
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            DataTable dtDF3Store = BaseDAO.ExecuteQuery("Select * from [242_DF3Store] where [MONo]='" + cbMONo.Text + "'");
            try
            {
                int.Parse(txtNGQtyF.Text);
                int.Parse(txtOKQtyF.Text);
            }
            catch (Exception)
            {

                MessageBox.Show("Số lượng OKQty hoặc NGQty không hợp lệ.");
                txtOKQtyF.Focus();
                return;
            }

            if (txtOKQtyF.Text == "")
            {
                MessageBox.Show("Ban phai nhap so luong OK! Neu khong co san pham OK ban nhap so luong bang 0");
                txtOKQtyF.Focus();
                return;
            }
            else if (txtNGQtyF.Text == "")
            {
                MessageBox.Show("Ban phai nhap so luong NG! Neu khong co san pham NG ban nhap so luong bang 0");
                txtNGQtyF.Focus();
                return;
            }
            else if (dtDF3Store.Rows.Count < 0)
            {
                MessageBox.Show("Xem lại số order.");
                cbMONo.Focus();
                //Hoi lai phan nay neu dtDF3Store.count <0 ????
                return;
            }
            else if (int.Parse(dtDF3Store.Rows[0]["STOKQTY"].ToString()) != int.Parse(txtOKQtyF.Text))
            {
                MessageBox.Show("Số lượng OKQTY không hợp lệ");
                txtOKQtyF.Focus();
                //Hoi lai phan nay neu dtDF3Store.count <0 ????
                return;
            }
            else if (int.Parse(dtDF3Store.Rows[0]["STNGQty"].ToString()) != int.Parse(txtNGQtyF.Text))
            {
                MessageBox.Show("Số lượng NGQty không hợp lệ");
                txtNGQtyF.Focus();
                return;
            }
            else
            {
                ClsMOTable MO = new ClsMOTable();
                clsAnodQtyChangeTable AnodQtyChange = new clsAnodQtyChangeTable(); //1
                ClsDAnodInspTable DAnodInsp = new ClsDAnodInspTable();//2
                ClsDBusInspTable DBusInsp = new ClsDBusInspTable();//3
                ClsDF3StoreTable DF3Store = new ClsDF3StoreTable();//4
                ClsDHandInspTable DHandInsp = new ClsDHandInspTable();//5
                ClsBusOderTable BusOder = new ClsBusOderTable();//6
                ClsDInspAnodTable DInspAnod = new ClsDInspAnodTable();//7
                ClsDInspF3Table DInspF3 = new ClsDInspF3Table();//8
                ClsDMachineHandTable DMachineHand = new ClsDMachineHandTable();//9
                ClsDMBusInspTable DMBusInsp = new ClsDMBusInspTable();//10
                ClsDMHandInspTable DMHandInsp = new ClsDMHandInspTable();//11
                ClsDMInspBusTable DMInspBus = new ClsDMInspBusTable();//12
                ClsDMInspHandTable DMInspHand = new ClsDMInspHandTable();//13
                ClsMOOptionTable MOOption = new ClsMOOptionTable();
                
                // update MO
                MO.MONo = cbMONo.Text;
                MO.OKQty = int.Parse(txtOKQtyF.Text);
                MO.NGQty = int.Parse(txtNGQtyF.Text);
                MO.Finishdate = dateDate.Value;
                clsMODAO.UpdateMOFinish(MO);

                //write Log
                log.Table = LogConstant.Table.Product.MOList;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + MO.MONo + "; OKQty: " + MO.OKQty + "; NGQty: " + MO.NGQty + "; Finishdate: " + MO.Finishdate;
                log.WriteLog();

                //Update vào các công đoạn
                //1
                AnodQtyChange.Mono = cbMONo.Text;
                clsMODAO.UpdateAnodQtyChangeMOFinish(AnodQtyChange);

                //write Log
                log.Table = LogConstant.Table.Product.Anod_Qty_Change;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + AnodQtyChange.Mono;
                log.WriteLog();

                //2
                DAnodInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDAnodInspMOFinish(DAnodInsp);

                //write Log
                log.Table = LogConstant.Table.Product.Anod_Insp;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + DAnodInsp.MONo;
                log.WriteLog();

                //3
                DBusInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDBusInspMOFinish(DBusInsp);

                //write Log
                log.Table = LogConstant.Table.Product.Bus_Insp;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + DBusInsp.MONo;
                log.WriteLog();

                //4
                DF3Store.MONo = cbMONo.Text;
                clsMODAO.UpdateDF3StoreMOFinish(DF3Store);

                //write Log
                log.Table = LogConstant.Table.Product.F3_Store;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + DF3Store.MONo;
                log.WriteLog();

                //5
                DHandInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDHandInspMOFinish(DHandInsp);

                //write Log
                log.Table = LogConstant.Table.Product.Hand_Insp;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //6
                BusOder.MONo = cbMONo.Text;
                BusOder.FinishDate = dateDate.Value;
                clsMODAO.UpdateBusOrderMOFinish(BusOder);

                //write Log
                log.Table = LogConstant.Table.Product.BusOrderList;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text + "; FinishDate: " + BusOder.FinishDate;
                log.WriteLog();

                //7
                DInspAnod.MONo = cbMONo.Text;
                clsMODAO.UpdateDInspAnodMOFinish(DInspAnod);

                //write Log
                log.Table = LogConstant.Table.Product.Insp_Anod;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //8
                DInspF3.MONo = cbMONo.Text;
                clsMODAO.UpdateDInspF3MOFinish(DInspF3);

                //write Log
                log.Table = LogConstant.Table.Product.Insp_F3;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //9
                DMachineHand.MONo = cbMONo.Text;
                clsMODAO.UpdateDMachineHandMOFinish(DMachineHand);

                //write Log
                log.Table = LogConstant.Table.Product.Machine_Hand;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //10
                DMBusInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDMBusInspMOFinish(DMBusInsp);

                //write Log
                log.Table = LogConstant.Table.Product.Bus_Insp;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //11
                DMHandInsp.MONo = cbMONo.Text;
                clsMODAO.UpdateDMHandInspMOFinish(DMHandInsp);

                //write Log
                log.Table = LogConstant.Table.Product.Hand_Insp;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //12
                DMInspBus.MONo = cbMONo.Text;
                clsMODAO.UpdateDMInspBusMOFinish(DMInspBus);

                //write Log
                log.Table = LogConstant.Table.Product.Insp_Bus;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //13
                DMInspHand.MONo = cbMONo.Text;
                clsMODAO.UpdateDMInspHandMOFinish(DMInspHand);

                //write Log
                log.Table = LogConstant.Table.Product.Insp_Hand;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text;
                log.WriteLog();

                //update vào MOOtion
                MOOption.MONo = cbMONo.Text;
                MOOption.Finishdate = dateDate.Value;
                clsMODAO.UpdateMOOptionMOFinish(MOOption);

                //write Log
                log.Table = LogConstant.Table.Product.MOOption;
                log.Task = LogConstant.Task.Update;
                log.Description = "MONo: " + cbMONo.Text + "; FinishDate: " + MOOption.Finishdate;
                log.WriteLog();

                //Reload(sender, e);
                MessageBox.Show("Đã Finished");
                txtMOQty.Text = "";
                txtNGQty.Text = "";
                txtNGQtyF.Text = "";
                txtOKQty.Text = "";
                txtOKQtyF.Text = "";
                cbMONo.Text = "";
                checkFinish.Checked = false;

                cbMONo.Focus();

            }
        }
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
